package com.dingreading.cloud.gateway.service.impl;

import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.gateway.entity.table.XcxStudentLoginTableDef;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.dingreading.cloud.gateway.entity.XcxStudentLogin;
import com.dingreading.cloud.gateway.mapper.XcxStudentLoginMapper;
import com.dingreading.cloud.gateway.service.XcxStudentLoginService;
import org.springframework.stereotype.Service;

/**
 * 小程序学生登录日志表 服务层实现。
 *
 * @author EDY
 * @since 2025-10-31
 */
@Service
public class XcxStudentLoginServiceImpl extends BaseServiceImpl<XcxStudentLoginMapper, XcxStudentLogin, XcxStudentLoginTableDef> implements XcxStudentLoginService {

    @Override
    protected XcxStudentLoginTableDef getTable() {
        return XcxStudentLoginTableDef.xcxStudentLogin;
    }

    @Override
    public XcxStudentLogin getLastLoginLogByMobile(String studentUid, String agencyUid, String loginTypeXcxMobile) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.studentUid.eq(studentUid))
                .and(table.agencyUid.eq(agencyUid))
                .and(table.device.eq(loginTypeXcxMobile))
                .and(table.expireTime.ge(DateUtil.getNowDatetime()));
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.id))
                .from(table)
                .where(condition);

        Long aLong = getMapper().selectObjectByQueryAs(wrapper, Long.class);
        Long id = NepUtil.nullToZero(aLong);

        return getMapper().selectOneByCondition(table.id.eq(id));
    }
}
